import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/login/index.vue'
import Home from '@/views/home/index.vue'
import { useUserStore } from '@/stores/modules/user.js'
import NotFound from '@/views/NotFound.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      redirect: '/dashboard',
      children: [
        {
          path: '/dashboard',
          component: () => import('@/views/home/echarts-page.vue')
        },
        {
          path: '/cate',
          component: () => import('@/views/cate/index.vue')
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue')
        },
        {
          path: '/mine/profile',
          component: () => import('@/views/mine/profile.vue')
        },
        {
          path: '/mine/avatar',
          component: () => import('@/views/mine/avatar.vue')
        },
        {
          path: '/mine/passwd',
          component: () => import('@/views/mine/passwd.vue')
        }
      ]
    },
    // plan on directly navigating to the not-found route using its name
    { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next()
    return
  }
  const userStore = useUserStore()
  if (userStore.token === '') {
    next('/')
    // next()
  } else {
    next()
  }
})
export default router
